#include "stdafx.h"
#include "Log.h"

__declspec(dllexport) int GetTotal()
{
	total++;
	return total;
}

__declspec(dllexport) int GetAge()
{
	return 28;
}

__declspec(dllexport) long OpenLogTagFile(LPCTSTR filePath)
{
	LOGTAG_INTERFACE m_Interface;
	LOGTAG_INFO	     m_Info;
	LPLOGTAG_SENSOR  m_pSensor = NULL;
	LPLOGTAG_READING m_pReadings = NULL;

	LOGTAG_HANDLE handle = LogTag_OpenAccess(0);
	if (handle == NULL) 
	{
		if (LogTag_LogOnUser(NULL, NULL, NULL) != LTR_SUCCESS)
			return -1;
		handle = LogTag_OpenAccess(0);
		if (handle == NULL)
			return -1;
	}

	memset(&m_Interface, 0, sizeof(m_Interface));
	m_Interface.cbSize = sizeof(m_Interface);
	memset(&m_Info, 0, sizeof(m_Info));
	m_Info.cbSize = sizeof(m_Info);
	if (m_pReadings != NULL)
	{
		delete[] m_pReadings;
		m_pReadings = NULL;
	}
	if (m_pSensor != NULL)
	{
        delete[] m_pSensor;
		m_pSensor = NULL;
	}

		if ((LogTag_OpenPath(handle, filePath) == LTR_SUCCESS) &&
		(LogTag_GetInterface(handle, &m_Interface) == LTR_SUCCESS) &&
		((m_Interface.wStatus & LTIS_LOGTAG) == LTIS_LOGTAG) &&
	    (LogTag_GetData(handle, &m_Info) == LTR_SUCCESS) &&
		(m_Info.dwNumOfReadings != 0))
	{
		m_pSensor = new LOGTAG_SENSOR[m_Info.wNumOfSensors];
		m_Info.pSensor = m_pSensor;
		m_Info.wSensorCount = m_pSensor ? m_Info.wNumOfSensors : 0;
		for (WORD nSensor = 0; nSensor != m_Info.wSensorCount; nSensor++)
			m_pSensor[nSensor].cbSize = sizeof(LOGTAG_SENSOR);

		m_pReadings = new LOGTAG_READING[m_Info.dwNumOfReadings];
		m_Info.pReading = m_pReadings;
		m_Info.dwReadingsCount = m_pReadings ? m_Info.dwNumOfReadings : 0;
		if (LogTag_GetData(handle, &m_Info) != LTR_SUCCESS)
			m_Info.dwNumOfReadings = 0;
	}

	LogTag_Close(handle);

	return 0;
}

__declspec(dllexport) long OpenLogTagInterface(LPCTSTR filePath, LOGTAG_INTERFACE *tagInterface)
{
	LOGTAG_HANDLE handle = LogTag_OpenAccess(0);
	if (handle == NULL) 
	{
		if (LogTag_LogOnUser(NULL, NULL, NULL) != LTR_SUCCESS)
			return -1;
		handle = LogTag_OpenAccess(0);
		if (handle == NULL)
			return -1;
	}

	LogTag_OpenPath(handle, filePath);
	LogTag_GetInterface(handle, tagInterface);

	return 0;
}




__declspec(dllexport) long __stdcall StringTest(char *value, int *number)
{
	strcpy_s(value, 255, "What's up Bitches??? 121 21212121212121212  Jou Ma!");
	*number = 42;
	return 0;
}

__declspec(dllexport) long __stdcall Test()
{
	return 0;
}
